home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / utility / fsco3712.zip / ENCODE_C.C < prev    next >
C/C++ Source or Header  |  1993-12-25  |  2KB  |  82 lines

  1. /***** Encode.c (25.12.93) by Flavio Stanchina *****/
  2.  
  3. #include <exec/types.h>
  4. #include <dos/dos.h>
  5. #include <dos/stdio.h>
  6.  
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9.  
  10. #if defined(__SASC)
  11. #define _USEOLDEXEC_
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14. #endif
  15.  
  16. #include "FSCode.h"
  17.  
  18. ULONG Encode(BPTR in, BPTR out, LONG size)
  19. {
  20.     LONG c1, c2;
  21.     ULONG tmp;
  22.     TEXT buf[6];
  23.     LONG c;
  24.  
  25.     ULONG crc = 0xFFFFFFFF; /* preload shift register, per CRC-32 spec */
  26.  
  27.     buf[5] = '\0';
  28.     c1 = c2 = 0;
  29.  
  30.     while((c1 + c2) < size)
  31.     {
  32.         if((c = FGetC(in)) == ENDSTREAMCH) break; /* Shouldn't happen */
  33.         c2++;
  34.         crc = crc32_stream(c, crc);
  35.         tmp = c;
  36.  
  37.         if((c = FGetC(in)) == ENDSTREAMCH) goto encode;
  38.         c2++;
  39.         crc = crc32_stream(c, crc);
  40.         tmp = tmp << 8 | c;
  41.  
  42.         if((c = FGetC(in)) == ENDSTREAMCH) goto encode;
  43.         c2++;
  44.         crc = crc32_stream(c, crc);
  45.         tmp = tmp << 8 | c;
  46.  
  47.         if((c = FGetC(in)) == ENDSTREAMCH) goto encode;
  48.         c2++;
  49.         crc = crc32_stream(c, crc);
  50.         tmp = tmp << 8 | c;
  51.  
  52. encode:
  53.         buf[4] = (tmp % 85) + 42; tmp /= 85;
  54.         buf[3] = (tmp % 85) + 42; tmp /= 85;
  55.         buf[2] = (tmp % 85) + 42; tmp /= 85;
  56.         buf[1] = (tmp % 85) + 42; tmp /= 85;
  57.         buf[0] = (tmp     ) + 42;
  58.  
  59.         if(c1 + c2 == size)
  60.             switch(size & 3) /* Equivalent to size % 4 for our purpose */
  61.             {
  62.                 case 1: buf[2] = '#';
  63.                 case 2: buf[1] = '#';
  64.                 case 3: buf[0] = '#';
  65.             }
  66.  
  67.         FPuts(out, buf);
  68.  
  69.         if(c2 == 60)
  70.         {
  71.             FPutC(out, '\n');
  72.             c1 += c2; /* Update counters */
  73.             c2  =  0;
  74.         }
  75.     }
  76.  
  77.     /* Need a newline? */
  78.     if(c2) FPutC(out, '\n');
  79.  
  80.     return crc;
  81. }
  82.